<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\admin;

use app\admin\controller\Admin;
use think\Db;
use app\common\builder\ZBuilder;
/**
 * 门店管理
 * @package app\cms\admin
 */
class Store extends Admin
{
    /**
     * 首页
     * @author shiqiang&dou
     * @return mixed
     */
    public function index(){	
		cookie('__forward__', $_SERVER['REQUEST_URI']);
		
		// 获取筛选
        $map = $this->getMap();
		
		//数据获取
        $data = input();
		$store_id = '';
		$uid = UID;//后台管理员ID
		$sotre_user = model('app\user\model\User') -> find($uid);//获取列表
		if($sotre_user['store_uid'] > 0){
			$map[] = ['id', '=', $sotre_user['store_id']];
			$store_id = $sotre_user['store_id'];
		}
		
		// 数据列表
        $data_list = model('ErpStore', 'model') -> where($map) -> order('sort asc, create_time desc') -> paginate();
		
		// 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('门店管理') // 设置页面标题
            ->setPrimaryKey('id') // 设置主键名为username
            ->setTableName('erp_store') // 设置数据表名
            ->addColumns([ // 批量添加列
                // ['__INDEX__', '序号'],
                ['id', '门店ID'],
                ['title', '门店名称', 'text.edit'],
                ['address', '门店地址', 'text.edit'],
				['code', '门店编码'],
                ['sort', '排序', 'text.edit'],
                ['create_time', '创建时间', 'datetime'],
                // ['store_status', '状态', 'switch'],
                ['right_button', '操作', 'btn']
            ])
			->setSearchArea([
                ['text', 'title', '门店名称', 'like'],
                ['text', 'address', '门店地址', 'like'],
                ['text', 'code', '门店编码', 'like'],
            ])
			->addTopButton('add', '', ['area' => ['50%', '80%']]) // 添加授权按钮
            ->addRightButton('custom', ['title' => '仓库管理', 'href' => url('erp/store_warehouse/index', ['store_id' => '__id__'])],true)
            ->addRightButton('custom', ['title' => '人员管理', 'href' => url('erp/store_user/index', ['store_id' => '__id__'])],true)
            ->addRightButton('edit', '', ['area' => ['50%', '80%']]) // 批量添加右侧按钮
            ->addRightButton('delete') // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }
	
	/**
     * 新增
     * @author shiqiang
     * @return mixed
     * @throws \think\Exception
     */
    public function add(){   
		// 保存数据
		if ($this->request->isPost()) {
            //数据接收
            $data 	= $this->request->post('', null, 'trim');
            $title 	= isset($data['title']) ? $data['title'] : '';
            $address = isset($data['address']) ? $data['address'] : '';
            $code 	= isset($data['code']) ? $data['code'] : '';
            $sort 	= isset($data['sort']) ? $data['sort'] : '';
            $lat 	= isset($data['lat']) ? $data['lat'] : 0;
            $lon 	= isset($data['lon']) ? $data['lon'] : 0;
            $mobile = isset($data['mobile']) ? $data['mobile'] : 0;
			
            //总仓分仓类别--暂无用预留
            /*$warehouse_id_zf = isset($data['warehouse_id_zf']) ? $data['warehouse_id_zf'] : '';
            $warehouse_id_f = isset($data['warehouse_id_f']) ? $data['warehouse_id_f'] : '';
            if($warehouse_id_f && $warehouse_id_z){
                $data['list_warehouse_fz'] = array_merge($warehouse_id_z, $warehouse_id_f);
                unset($data['warehouse_id_f'], $data['warehouse_id_z']);
            }elseif ($warehouse_id_f || $warehouse_id_z) {
                $data['list_warehouse_fz'] = $warehouse_id_f?$warehouse_id_f:$warehouse_id_z;
                if($warehouse_id_f){
                    unset($data['warehouse_id_f']);
                }
                if($warehouse_id_z){
                    unset($data['warehouse_id_z']);
                }
            } else{
                $data['list_warehouse_fz'] = '';
            }*/
			// $warehouse_id = isset($data['warehouse_id_zf']) ? $data['warehouse_id_zf'] : '';
			
			//数据验证
			$Validate = validate('Store', 'validate');
			if( !$Validate -> scene('store_add') -> check($data) ){
				$this->error($Validate -> getError());
			}

			$rel = model('ErpStore','model') -> addData($title,$address, $sort, $code, $lat, $lon, $mobile);

			if($rel['code'] == 200){
				$this->success('新增成功', '', '_parent_reload');
			}else{
				$this->error($rel['message']);
			}

		}
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增门店') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['text', 'title', '门店名称'],
                ['text', 'code', '门店编码', '必填，可由英文字母、数字组成'],
                ['text', 'address', '门店地址', '请输入门店详细地址'],
                ['text', 'mobile', '门店电话', '请输入门店电话'],
				['text', 'lon', '经度', '请输入门店经度'],
                ['text', 'lat', '纬度', '请输入门店纬度'],
            ])
            ->addText('sort', '排序', '', 100)
            ->fetch();
    }
	
	/**
     * 编辑
     * @author shiqiang <976162041@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function edit($id = ''){   
		if ($id === '') $this->error('缺少参数');
		// 保存数据
		if ($this->request->isPost()) {
			$data = $this->request->post('', null, 'trim');
			$data['id'] = $id;
			$title      = isset($data['title']) ? $data['title'] : '';
			$address   	= isset($data['address']) ? $data['address'] : '';
			$code  		= isset($data['code']) ? $data['code'] : '';
			$sort  		= isset($data['sort']) ? $data['sort'] : '';
			$lat 	= isset($data['lat']) ? $data['lat'] : 0;
            $lon 	= isset($data['lon']) ? $data['lon'] : 0;
            $mobile = isset($data['mobile']) ? $data['mobile'] : 0;

			//数据验证
			$Validate = validate('Store', 'validate');
			if(!$Validate -> scene('store_edit') -> check($data)){
				$this->error($Validate -> getError());
			}
			
			$rel = model('ErpStore','model') -> editData($id, $title, $address, $sort, $code, $lat, $lon, $mobile);
			if($rel['code'] == 200){
				$this->success('编辑成功', '', '_parent_reload');
			}else{
				$this->error($rel['message']);
			}

		}
		
		$info = model('ErpStore', 'model') -> find($id);
		
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑门店')
            ->addFormItems([
				['text', 'title', '门店名称'],
                ['text', 'code', '门店编码', '必填，可由英文字母、数字组成'],
                ['text', 'address', '门店地址', '请输入门店详细地址'],
				['text', 'mobile', '门店电话', '请输入门店电话'],
				['text', 'lon', '经度', '请输入门店经度'],
                ['text', 'lat', '纬度', '请输入门店纬度'],
            ])
			->addText('sort', '排序')
			->setFormData($info)
            ->fetch();
    }
}

